package com.cw.jeeyt.service.directive;

import java.io.Writer;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.cw.jeeyt.common.enums.EnumBtnInfo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.model.security.SecurityResourceDo;
import com.cw.jeeyt.service.impl.cache.OperationCache;
import com.cw.lang.common.framework.utils.SpringContextUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateModel;

/**
 * 按钮权限自定义标签
 *
 * @author chenw
 * @version 1.0.0
 */
public class PermissionDirective implements TemplateDirectiveModel {
    private static final Logger logger = LoggerFactory.getLogger(PermissionDirective.class);

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) {
        Writer out = env.getOut();
        env.setOutputEncoding("utf-8");
        try {
            OperationCache operationCache = SpringContextUtil.getBean(OperationCache.class);
            String user_permissions = SessionUtil.getUserRoles();
            TemplateModel btnClass = (TemplateModel) params.get("btnClass");
            TemplateModel btnType = (TemplateModel) params.get("btnType");
            TemplateModel btnId = (TemplateModel) params.get("btnId");
            TemplateModel btnMethod = (TemplateModel) params.get("btnMethod");
            TemplateModel btnLink = (TemplateModel) params.get("btnLink");
            TemplateModel btnValue = (TemplateModel) params.get("btnValue");
            //TemplateModel iconClass = (TemplateModel) params.get("iconClass");
            TemplateModel op = (TemplateModel) params.get("op");
            boolean hasPermission = false;
            String[] permList = user_permissions.split(",");
            if (permList != null && permList.length > 0) {
                for (String perm : permList) {
                    List<SecurityResourceDo> resourceList = (List<SecurityResourceDo>) operationCache.getOperations(
                        perm);
                    String resourceInfo = JSON.toJSONString(resourceList);
                    if (!StringUtils.isEmpty(resourceInfo)) {
                        if (resourceInfo.contains(op.toString())) {
                            hasPermission = true;
                            break;
                        }
                    }
                }
            }
            //如果有权限
            if (hasPermission) {
                StringBuffer sb = new StringBuffer("");
                //超链接
                if (btnLink != null && "true".equals(btnLink.toString())) {
                    sb.append("<a href='' style='margin-right:3px' class='js-del-row-btn'");
                    if (btnId != null) {
                        sb.append(" id='" + btnId.toString() + "'");
                    }
                    sb.append(">");
                    if (btnValue != null) {
                        sb.append(btnValue.toString());
                    }
                    sb.append("</a>");
                    out.write(sb.toString());
                    return;
                }
                sb.append("<button type='button' ");
                if (btnClass == null) {
                    sb.append("class='btn btn-primary' ");
                } else {
                    sb.append("class='" + btnClass.toString() + "' ");
                }
                if (btnId != null) {
                    sb.append("id='" + btnId.toString() + "' ");
                }
                if (btnMethod != null) {
                    sb.append("onclick='" + btnMethod.toString() + "'>");
                } else {
                    sb.append("onclick=''>");
                }
                if(btnType != null){
                    if (EnumBtnInfo.ADD.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-plus'>&nbsp;&nbsp;" + EnumBtnInfo.ADD.getDescription());
                    } else if (EnumBtnInfo.EDIT.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-edit'>&nbsp;&nbsp;" + EnumBtnInfo.EDIT.getDescription());
                    } else if (EnumBtnInfo.VIEW.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-eye'>&nbsp;&nbsp;" + EnumBtnInfo.VIEW.getDescription());
                    } else if (EnumBtnInfo.SAVE.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-save'>&nbsp;&nbsp;" + EnumBtnInfo.SAVE.getDescription());
                    } else if (EnumBtnInfo.SUBMIT.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-save'>&nbsp;&nbsp;" + EnumBtnInfo.SUBMIT.getDescription());
                    } else if (EnumBtnInfo.REMOVE.getCode()
                        .equals(btnType.toString())) {
                        sb.append(
                            "<i class='fa fa-trash-o'>&nbsp;&nbsp;" + EnumBtnInfo.REMOVE.getDescription());
                    } else if (EnumBtnInfo.FOLDER.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-folder-open-o'>&nbsp;&nbsp;" + EnumBtnInfo.FOLDER.getDescription());
                    } else if (EnumBtnInfo.CONFIG.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-gears'>&nbsp;&nbsp;" + EnumBtnInfo.CONFIG.getDescription());
                    } else if (EnumBtnInfo.DOWNLOAD.getCode()
                        .equals(btnType.toString())) {
                        sb.append(
                            "<i class='fa fa-arrow-circle-down'>&nbsp;&nbsp;" + EnumBtnInfo.DOWNLOAD.getDescription());
                    } else if (EnumBtnInfo.IMPORT.getCode()
                        .equals(btnType.toString())) {
                        sb.append("<i class='fa fa-arrow-circle-up'>&nbsp;&nbsp;" + EnumBtnInfo.IMPORT.getDescription());
                    } else {
                        if (btnValue != null) {
                            sb.append(btnValue.toString());
                        }else{
                            sb.append("<i>&nbsp;&nbsp;未知按钮");
                        }
                    }
                }else{
                    if (btnValue != null) {
                        sb.append(btnValue.toString());
                    }
                }
                sb.append("</i>");
                sb.append("</button>");
                out.write(sb.toString());
            } else {
                logger.error("当前用户无【" + op.toString() + "】操作权限");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("处理页面控制按钮权限出错：{}", e.getMessage());
        }
    }

}
